// pages/doctor-goods/doctor-goods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize: 5,
    pageNum: 1,
    goodslist: [],
    imgurl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl, // 本地图片
    upstate: 0,
    downstate: 0,
    num:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
  },

  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    this.loadData();
  },

  // 加载医美产品数据
  loadData: function() {
    app.showLoading();
    var that = this;
    app.request({
      url: 'doctor/product/getPage',
      data: {
        'pageSize': that.data.pageSize,
        'pageNum': that.data.pageNum,
        'doctorId': wx.getStorageSync('userid'),
      },
      success(res) {
        wx.hideLoading();
      
        that.setData({
          goodslist: res.data,
          num:res.page.count
        })

      }
    })
  },

  // 上下架商品接口
  updateState: function(state, id) {

    var that = this;
    app.request({
      url: 'doctor/product/updateState',
      data: {
        'goodId': id,
        'type': state,
      },
      success(res) {
        that.setData({
          'pageSize': 5,
          'pageNum': 1,
        })
        that.loadData();
      }
    })
  },

  // 处理上架逻辑
  handleUp: function(e) {
    var that = this;
    wx.showModal({
      title: '注意!',
      content: '确认上架?',
      confirmText: '是',
      cancelText: '否',
      success: function(res) {
        if (res.cancel) {} else {
          //点击确定 
          var goodsid = e.target.dataset.goodsid;
          that.updateState(1, goodsid);
        }
      },
      fail: function(res) {},
    })
  },

  // 处理下架逻辑
  handleDown: function(e) {
    var that = this;
    wx.showModal({
      title: '注意!',
      content: '确认下架?',
      confirmText: '是',
      cancelText: '否',
      success: function(res) {
        if (res.cancel) {} else {
          //点击确定 
          var goodsid = e.target.dataset.goodsid;
          that.updateState(2, goodsid);
        }
      },
      fail: function(res) {},
    })
  },

  // 处理删除逻辑
  handleDelete: function(e) {
    const state = e.currentTarget.dataset.state;
 
    var that = this;
    if (state == 1) {
      app.showModal("请先下架该产品");
    } else {
      wx.showModal({
        title: '注意!',
        content: '确认删除?',
        confirmText: '是',
        cancelText: '否',
        success: function(res) {
          if (res.cancel) {} else {
            //点击确定 
            var goodsid = e.target.dataset.goodsid;
            that.updateState(3, goodsid);
          }
        },
        fail: function(res) {},
      })
    }
  },

  // 处理编辑
  handleEdit: function(e) {

    var goodsid = e.target.dataset.goodsid;
    wx.navigateTo({
      url: '../doctor-goods-edit-2/doctor-goods-edit-2?id=' + goodsid,
    })
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let { num, goodslist}=this.data;
    wx.showLoading({
      title: '加载中...',
    })
    if (goodslist.length == num) {
     
      wx.showToast({
        title: '没有更多了哦~',
        icon: 'none',
        duration: 1000
      });
    } else{
      this.setData({
        'pageSize': this.data.pageSize + 5,
      })
      this.loadData();
    }
    
  },

})